package com.waiterxiaoyy.util;

import com.aliyuncs.exceptions.ClientException;
import com.waiterxiaoyy.entity.Log;
import com.waiterxiaoyy.entity.Notice;
import com.waiterxiaoyy.entity.Renew;
import com.waiterxiaoyy.mapper.MailMapper;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.rmi.server.ExportException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class ZhaoShengJsoup {
    public static Date basiTime = new Date(2020-1900,9-1,19);

    @Autowired
    private MailMapper mailMapper;
    @Autowired
    private SendEmail sendEmail;

//    @Scheduled(cron = "0 0/2 * * * ?")
//    @Scheduled(cron = "0/50 * * * * ? ")//每50秒执行一次
    @Scheduled(cron="0 0/30 * * * ?")//每10分钟
    public void task() throws Exception {
        Connection connection1 = Jsoup.connect("https://yzw.gdut.edu.cn/sszs.htm")
                .header("Accept",
                        "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36")
                .method(Connection.Method.GET)
                .timeout(3000);
        Connection.Response response1 = connection1.execute();
        Document document = response1.parse();

        Elements lis = document.getElementsByTag("ul").get(1).getElementsByTag("li");
        Notice notice = null;

        //获取当前时间
        Date date1 = new Date();
        String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date1);

        //插入更新表
        Renew renew = new Renew();
        renew.setRenewTime(nowTime);
        mailMapper.renewInsert(renew);

        ArrayList<Notice> newNotice = new ArrayList<>();

        for(int i = lis.size() - 1; i >= 0 ; i--) {
            notice = new Notice();

            String title = lis.get(i).getElementsByTag("a").attr("title").toString();
            String noticeUrl = "https://yzw.gdut.edu.cn/" + lis.get(i).getElementsByTag("a").attr("href");
            String date = lis.get(i).getElementsByTag("span").get(0).text().toString();

            notice.setDate(date);
            notice.setNoticeUrl(noticeUrl);
            notice.setNoticeTitle(title);
            notice.setZhaoshengUrl("https://yzw.gdut.edu.cn/sszs.htm");
            notice.setCreateTime(nowTime);

            if(mailMapper.existNotice(notice.getNoticeTitle()) != 1) {
                newNotice.add(notice);
                if(mailMapper.addNotice(notice) == 1) {
                    Log log = new Log();
                    log.setMessage("【新通知加入】" + notice.getNoticeTitle());
                    log.setDateTime(nowTime);
                    mailMapper.addLog(log);
                }
            }
        }

        if(newNotice.size() > 0) {
            sendEmail.sendEmail(newNotice);
        }
    }
}
